/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.basics.editor.gui.dialog;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import com.genia.toolbox.basics.editor.gui.AbstractGUI;

/**
 * Gui tools to choose files or folders on the system.
 */
public class FileChoosers
{

  /**
   * The current fle chooser.
   */
  public static FileChoosers choosers = null;

  /**
   * The application gui.
   */
  private static AbstractGUI<?, ?, ?, ?, ?, ?> gui = null;



  /**
   * Constructor.
   * 
   * @param gui
   *          The application gui.
   */
  public FileChoosers(AbstractGUI<?, ?, ?, ?, ?, ?> gui)
  {
    super();
    FileChoosers.gui = gui;
  }



  /**
   * Get the current instance of the file chooser.
   * 
   * @param gui
   *          the application gui.
   * @return the curretn file chooser.
   */
  public static FileChoosers getInstance(AbstractGUI<?, ?, ?, ?, ?, ?> gui)
  {
    if (choosers == null) {
      choosers = new FileChoosers(gui);
    }
    return choosers;
  }



  /**
   * Choose a file for saving purpose.
   * 
   * @return a file.
   */
  public File saveToJspFile()
  {
    return this.selectFile(true, "jsp");
  }



  /**
   * Chooses a folder for saving purpose.
   * 
   * @return a file.
   */
  public File saveToJspFolder()
  {
    return this.selectFolder(true);
  }



  /**
   * Choose a file for saving purpose.
   * 
   * @return a file.
   */
  public File saveToXMLFile()
  {
    return this.selectFile(true, "xml");
  }



  /**
   * Choose a file for loading purpose.
   * 
   * @return a file.
   */
  public File openToXMLFile()
  {
    return this.selectFile(false, "xml");
  }



  /**
   * Select a file with unique extension file filter.
   * 
   * @param save
   *          true if we're going to use the file for saving purpose, false if
   *          it is to load the content of the file.
   * @param extension
   *          the file filter extension
   * @return the selected file.
   */
  private File selectFile(boolean save, final String extension)
  {
    File file = null;
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

    FileFilter filter = new FileFilter()
    {
      @Override
      public boolean accept(File f)
      {
        return f.isDirectory() || f.getName().endsWith("." + extension);
      }



      @Override
      public String getDescription()
      {
        return extension.toUpperCase() + " File.";
      }
    };
    chooser.setFileFilter(filter);
    int option;
    if (save) {
      option = chooser.showSaveDialog(gui.getFrame());
    }
    else {
      option = chooser.showOpenDialog(gui.getFrame());
    }
    if (option == JFileChooser.APPROVE_OPTION) {
      file = chooser.getSelectedFile();
      if (!file.getAbsolutePath().endsWith("." + extension)) {
        file = new File(file.getAbsolutePath() + "." + extension);
      }
    }
    return file;
  }



  /**
   * Select a folder.
   * 
   * @param save
   *          true if we're going to use the file for saving purpose, false if
   *          it is to load the content of the file.
   * @return the selected file.
   */
  private File selectFolder(boolean save)
  {
    File file = null;
    JFileChooser chooser = new JFileChooser();
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

    FileFilter filter = new FileFilter()
    {
      @Override
      public boolean accept(File f)
      {
        return f.isDirectory();
      }



      @Override
      public String getDescription()
      {
        return "Folder.";
      }
    };
    chooser.setFileFilter(filter);
    int option;
    if (save) {
      option = chooser.showSaveDialog(gui.getFrame());
    }
    else {
      option = chooser.showOpenDialog(gui.getFrame());
    }
    if (option == JFileChooser.APPROVE_OPTION) {
      file = chooser.getSelectedFile();
    }
    return file;
  }

}
